package com.wits.platform.web.controller;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping(value="/icon")
public class IconController extends BaseController{

	private static List<String> iconList=null;
	
	
	@ResponseBody
	@RequestMapping(value="/getIcons")
	public Object getIcons(HttpServletRequest req){
		if(iconList==null || iconList.size()==0){
			String path=req.getSession().getServletContext().getRealPath("/");
			iconList=new ArrayList<String>();
			File file=new File(path+File.separator+"static"+File.separator+"icons"+File.separator+"32X32");
			readIcons(file);
		}
		return ajaxDoneSuccess(iconList);
	}
	
	
	private void readIcons(File file) {
//		File[] children = file.listFiles(new FilenameFilter() {
//			@Override
//			public boolean accept(File dir, String name) {
//				return name.matches("(.*?)\\.(?i)(gif|png)");
//			}
//		});
		String path;
		for (File f : file.listFiles()) {
			if (f.isDirectory()) {
				readIcons(f);
			} else {
				path=f.getPath();
				iconList.add(path.substring(path.indexOf("static")));
			}

		}
	}
	
	
	
}
